<?php
include("/var/www/html/new/d/common/auth.php");
include("/var/www/html/new/d/common/errBack.php");
include("/var/www/html/new/d/common/err_code.php");
include("/var/www/html/new/d/class/ProductInfo.php");
include("/var/www/html/new/d/class/Member.php");
include("/var/www/html/new/d/class/DisOrder.php");
include("/var/www/html/new/d/class/DisOrder_H.php");
include("/var/www/html/new/d/class/abc/Product_H.class.php");
include("/var/www/html/new/d/class/Sale.class.php");
include("/var/www/html/new/d/module/common/Db.class.php");

$_REQUEST['c_pids'] = $c_pids = array_filter($_REQUEST['c_pids']);
$_REQUEST['pid']    = $pid    = key($c_pids);
$_REQUEST['tnum']   = $tnum   = current($c_pids);

// 组合生成座位依据
$seat_data = array();
foreach($_REQUEST['c_pids'] as $key=>$ticket_num){
	$seat_data[$key] = array(
		'ticket_num' => $ticket_num,
		'area_id'    => $_REQUEST['c_area'][$key],
	);
}
unset($_REQUEST['c_pids'][$pid]);

// 基本信息检查
if(p_match($begintime=$_REQUEST['begintime'])) locationBack('游玩日期格式不正确');
if(p_match($ordername=$_REQUEST['ordername'])) locationBack('取票姓名含有非法字符');
if(p_match($ordertel = $_REQUEST['ordertel'])) locationBack('取票人手机格式不正确');
if(($tnum=$_REQUEST['tnum'] + 0)==0) locationBack('取票姓名含有非法字符');
if(($venue_id=$_REQUEST['venus_id']+0)==0) locationBack('景区未关联演出场馆');
if(($round_id=$_REQUEST['round_id']+0)==0) locationBack('未选择预定场次');


$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));



$memberID = $_SESSION['sid'];


if(!$pid=abs($_REQUEST['pid'])){ getOut("index.html");}
$aid=abs($_REQUEST['aid']);
$pro = new ProductInfo($s,$pid,$aid);
$m = new Member($s,$memberID);
$disOrder = new DisOrder_H($s,$pro,$m);



$totalTnum=$tnum;
foreach((array)$_REQUEST['c_pids'] as $c_pid=>$c_num){
	$totalTnum+=$c_num;
}


$tourists=array();
$idcards=array();
for($n=1;$n<($totalTnum+1);$n++){
	if($_REQUEST['tourist_name_'.$n] && $_REQUEST['tourist_cert_'.$n]){
		$tourists[]=mysql_real_escape_string($_REQUEST['tourist_name_'.$n]);
		$idcards[]=mysql_real_escape_string($_REQUEST['tourist_cert_'.$n]);
	}
}
$option=array();
$option["pid"]=$pid;
$option["begintime"] = $begintime;
$option["leavetime"] = $_REQUEST['leavetime'];
$option["ordername"] = trim($ordername);
$option["ordertel"]  = $ordertel;
$option["tnum"]      = $tnum;
$option["remoteorder"]=$remoteorder;
$option["c_pids"]   = array();
$option["from"]     = $_REQUEST['from'];
$option['memo']     = mysql_real_escape_string($_REQUEST['memo']);
$option['origin']   = $_REQUEST['origin'];
$option['tourists'] = $tourists;
$option['idcards']  = $idcards;
$option['paymode']  = abs($_REQUEST['paymode']);
$option['series']   = json_encode(array($venue_id, $round_id, $_REQUEST['c_area'][$pid])); // 团号;

if(count($idcards)) $option['sfz'] = $idcards[0];
if(isset($_REQUEST['sfz'])){$option['sfz']=$_REQUEST['sfz'];}

// print_r($option);
// exit;

try{
	
	$r=$disOrder->order($option, $aid);
	write_logs(json_encode($r));
	$url="orderSuc.html?ordernum=".$r['orderNum'] . '&ot=' . md5($r['orderType']) ;
	if($_SESSION['dtype']==6){
		$daction="预订订单($r)";
		$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
		$GLOBALS['le']->query($str);
	}
}catch(DisOrderException $e){
	getOut("plist.html",$e->getMessage());
}catch(InterFaceException $e){
	$url="orderFailure.html?msg=".$e->getMessage()."：".$SoapRequestChk[$e->getCode()]."(".$e->getCode().")";
}catch(MsgException $e){
	$url="orderFailure.html?msg=".$e->getMessage()."：".telcode($e->getCode())."(".$e->getCode().")";
}
if($_REQUEST['saveTo']==1){//常用联系人存储
	$m->addCinfo($ordername,$ordertel);
}
echo "Location:$url";
header("Location:$url");

function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}

function write_logs($txt,$file="/var/www/html/new/d/logs/orderInfo_logs.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

function locationBack($messgae){
	exit('<script>alert('.$messgae.');javascript:history.go(-1);</script>');
}